package rule01.jdbc;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChangLiang
 * @date 2020/3/16
 */
public class DriverManager {

    private static final List<Driver> drivers = new ArrayList<>();

    public static void registerDriver(Driver driver) {
        drivers.add(driver);
    }

    /**
     * 这就是一个静态工厂方法
     * 静态工厂返回的类，可以在编写静态工厂时根本不存在
     * @param url
     * @param username
     * @param password
     * @return
     */
    public static Connection getConnection(String url, String username, String password) {
        for (Driver driver : drivers) {
            Connection connection = driver.getConnection(url, username, password);
            if (connection != null) {
                return connection;
            }
        }
        return null;
    }
}
